home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_10 / mueller / xassert.h < prev   
Encoding:
C/C++ Source or Header  |  1994-08-23  |  3.3 KB  |  102 lines

  1.  
  2. // QUANTIFIERS IN ASSERTIONS FOR C++     - (c) HMMueller 1993
  3.  
  4. // Author:      Harald M. Mueller
  5. //              Siemens AG Oesterreich
  6. //              Hainburgerstr. 33
  7. //              A-1030 Wien / Austria
  8. // email:       mueller@garwein.hai.siemens.co.at
  9.  
  10. // All the following code was written outside my work at Siemens AG
  11. // Oesterreich. Siemens has nothing to do with it.
  12.  
  13. // Remark: This file is not at all commented. This is not good programming
  14. // practice, but as the following code is quite intricate, I decided to
  15. // put the information about its inner workings - and also about its use
  16. // - in a separate document, called "Powerful Assertions for C++ Without
  17. // Language Extensions".
  18.  
  19. #ifndef _ASSERT_H_
  20. #define _ASSERT_H_
  21.  
  22. #include <stream.h>
  23.  
  24. static int asstInvert, asstResult, asstEval, asstShortCut;
  25.  
  26. static struct AssertCt {
  27.     // empty
  28. } asstCt;
  29.  
  30. inline AssertCt& anAssertCt() {
  31.     asstInvert = 0;
  32.     asstEval = 1;
  33.     return asstCt;
  34. }
  35.  
  36. inline int asstDoEval(int& asstShortCut) {
  37.     int eval = asstEval;
  38.     asstShortCut = ! asstEval && asstResult;
  39.     asstEval = 0;
  40.     return eval;
  41. }
  42.  
  43. inline const AssertCt& operator!(const AssertCt& a)
  44. {
  45.     asstInvert = !asstInvert;
  46.     return a;
  47. }
  48.  
  49. inline int operator&&(int left, const AssertCt&)
  50. {
  51.     asstEval = left;
  52.     return left;
  53. }
  54.  
  55. inline int operator||(int left, const AssertCt&)
  56. {
  57.     asstEval = !left;
  58.     return left;
  59. }
  60.  
  61. static ostream& assertout(ostream& os, unsigned line, char* file)
  62. {
  63.     return os << "Assertion in line " << line << ", file "
  64.               << file << " failed for ";
  65. }
  66.  
  67. // ----------------------------------------------------------------------
  68.  
  69. #define asstLoopTest(asstFor,asstAll,asstCond)                  \
  70.         anAssertCt();                                           \
  71.         { int asstShortCut;                                     \
  72.           if (asstDoEval(asstShortCut)) {                       \
  73.               int asstInvert = ::asstInvert;                    \
  74.               asstResult = asstAll;                             \
  75.               for asstFor {                                     \
  76.                   asstResult = 0 || asstCond;                   \
  77.                   if (asstResult != asstAll) break;             \
  78.               }                                                 \
  79.               if (asstInvert) asstResult = !asstResult;         \
  80.           }                                                     \
  81.           ::asstShortCut = asstShortCut;                        \
  82.         }                                                       \
  83.         asstResult = ::asstShortCut ? asstResult : asstResult
  84.  
  85. #define assertp(asstCond,asstOutput)                            \
  86.         asstResult = 0 || asstCond, asstResult ||               \
  87.         (assertout(ASSERTSTREAM,__LINE__,__FILE__) << asstOutput\
  88.         << endl, ASSERTACTION, 0)
  89.  
  90. #define assert(asstCond) assertp(asstCond,"condition \`" #asstCond "\'")
  91.  
  92. #define forall(asstFor,asstCond)    asstLoopTest(asstFor,1,asstCond)
  93. #define exists(asstFor,asstCond)    asstLoopTest(asstFor,0,asstCond)
  94.  
  95. #define old(asstname)                   old_##asstname
  96. #define saveold(assttype,asstname)      assttype old_##asstname = asstname
  97.  
  98. #define ASSERTSTREAM    cerr
  99. #define ASSERTACTION    exit(125)
  100.  
  101. #endif
  102.